//
//  HomeReusableView.m
//  jingdongfang
//
//  Created by LJL on 2018/5/7.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeReusableView.h"

@interface HomeReusableView()
{
    UIView *lineView;
}
@property (strong, nonatomic) UILabel *typeL;
@property (strong, nonatomic) UIButton *moreBtn;
@property (strong, nonatomic) UIImageView *yellowV;
@property (strong, nonatomic) UIImageView *lineV;
@end

@implementation HomeReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {

    }
    return self;
}

//- (UILabel *)typeL
//{
//    if (!_typeL)
//    {
//
//    }
//    return _typeL;
//}

- (UIButton *)moreBtn
{
    if (!_moreBtn)
    {
        self.moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setImage:[UIImage imageNamed:@"home_more"] forState:UIControlStateNormal];
        [_moreBtn setImage:[UIImage imageNamed:@"home_more"] forState:UIControlStateSelected];
        [_moreBtn addTarget:self action:@selector(moreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (UIImageView *)yellowV
{
    if (!_yellowV)
    {
        self.yellowV = [[UIImageView alloc] initWithFrame:CGRectZero];
        _yellowV.image = [UIImage imageNamed:@"home_2"];
    }
    return _yellowV;
}

- (UIImageView *)lineV
{
    if (!_lineV)
    {
        self.lineV = [[UIImageView alloc] initWithFrame:CGRectZero];
        _lineV.image = [UIImage imageNamed:@"home_1"];
    }
    return _lineV;
}

- (void)setModel:(IGHomeReusableModel *)model
{
    if (_model != model)
    {
        for (UIView *view in self.subviews)
        {
            [view removeFromSuperview];
        }
        _model = model;
        self.typeL = [[UILabel alloc] initWithFrame:CGRectZero];
        self.typeL.font = [UIFont font18];
        self.typeL.textColor = [UIColor c4Color];
        self.typeL.textAlignment = NSTextAlignmentCenter;
        self.typeL.text = model.titleStr;
        self.moreBtn.hidden = (model.type == normalType) ? NO : YES;
        [self.typeL sizeToFit];
        lineView = [UIView new];
        lineView.backgroundColor = [UIColor mainBackgroundColor];
        [self addSubview:lineView];
        [self addSubview:self.typeL];
        [self addSubview:self.moreBtn];
        [self addSubview:self.yellowV];
        [self addSubview:self.lineV];
//        self.typeL.backgroundColor = [UIColor cyanColor];        
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.backgroundColor = [UIColor c3Color];
    @KWeakObj(self);
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(19);
        make.centerY.mas_equalTo(selfWeak);
        make.right.mas_equalTo(-15);
    }];
   
    [self.typeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_model.titleWid + 10);
        make.height.mas_equalTo(20);
        make.center.mas_equalTo(selfWeak);
    }];
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(11);
        make.bottom.mas_equalTo(selfWeak.typeL).offset(-2.5);
        make.left.mas_equalTo(selfWeak.typeL).offset(_model.titleWid + 15);
    }];
    
    [self.yellowV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(11);
        make.bottom.mas_equalTo(selfWeak.typeL).offset(-2.5);
        make.right.mas_equalTo(selfWeak.typeL).offset(-15 - _model.titleWid);
    }];
}

- (void)moreBtnClick
{
    self.block();
}

@end
